<template>
  <div class="search_box">
    <lay-select
      v-model="projectId"
      size="xs"
      placeholder="请选择项目"
      :allow-clear="true"
      @change="changePrject"
      class="w"
    >
      <lay-select-option
        v-for="(item, index) in projectList"
        :key="item.testItemId"
        :value="item.testItemId"
        :label="item.testItemName"
      ></lay-select-option>
    </lay-select>

    <lay-input
      v-model="searchMsg.projectName"
      class="w"
      size="xs"
      :allow-clear="true"
      placeholder="请输入工程名称"
    ></lay-input>

    <lay-input
      v-model="searchMsg.constructionPosition"
      class="w"
      size="xs"
      :allow-clear="true"
      placeholder="请输入施工部位"
    ></lay-input>

    <lay-input
      v-model="searchMsg.client"
      class="w"
      size="xs"
      :allow-clear="true"
      placeholder="请输入委托单位"
    ></lay-input>

    <lay-input
      v-model="searchMsg.entrustmentNo"
      class="w"
      size="xs"
      :allow-clear="true"
      placeholder="请输入委托编号"
    ></lay-input>

    <lay-input
      v-model="searchMsg.reportNo"
      class="w"
      size="xs"
      :allow-clear="true"
      placeholder="请输入报告编号"
    ></lay-input>

    <lay-input
      v-if="tabs == 5"
      v-model="searchMsg.manager"
      class="w"
      size="xs"
      :allow-clear="true"
      placeholder="请输入报告管理人"
    ></lay-input>

    <lay-date-picker
      v-model="time"
      range
      class="w"
      size="xs"
      :allow-clear="true"
      :placeholder="['开始日期','结束日期']"
    ></lay-date-picker>

    <lay-button
      @click="submitClick"
      class="btn"
      type="primary"
      size="sm"
    >查询
    </lay-button>

    <lay-button
      @click="reactInput"
      type="primary"
      size="sm"
    >重置
    </lay-button>

  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, ref, toRefs, onMounted } from "vue";
import { layer } from "@layui/layer-vue";
import { GetTestItemInfo } from "../../../../api/module/user";
export default defineComponent({
  props: ['tabs'],
  setup(prop, event) {
    const model = reactive({
      searchMsg: {
        sampleReceivingBTime: null,
        sampleReceivingETime: null,
      } as any,
      time: [],
      projectList: [] as any,
      projectId: "" as any,
      manager: '' as any,
    });

    const submitClick = () => {
      let obj = {
        sampleReceivingBTime: null as any,
        sampleReceivingETime: null as any,
      }

      obj.sampleReceivingBTime = model.time[0];
      obj.sampleReceivingETime = model.time[1];

      if (obj.sampleReceivingBTime == '' && !obj.sampleReceivingBTime.length) {
        model.searchMsg.sampleReceivingBTime = null;
        model.searchMsg.sampleReceivingETime = null;
      } else {
        model.searchMsg.sampleReceivingBTime = obj.sampleReceivingBTime + 'T00:00';
        model.searchMsg.sampleReceivingETime = obj.sampleReceivingETime + 'T23:59';
      }
      console.log('model', model);
      
      event.emit("search", model.searchMsg);
    };

    const reactInput = () => {
      for (let key in model.searchMsg) {
        model.searchMsg[key] = "";
      }
      model.searchMsg.sampleReceivingBTime = null;
      model.searchMsg.sampleReceivingETime = null;
      model.searchMsg.testItems = null;
      model.projectId = "";
      model.time = [];
      event.emit("search", model.searchMsg);
    };

    const getProjectList = async () => {
      if (httpUrl.value.includes("/Setting/GetTestItemInfo")) { 
        const res: any = await GetTestItemInfo({
          pageIndex: 1,
          pagePerCount: 2500,
          condition: '',
        });
        if (res.code == 200) {
          model.projectList = res.data.datas;
        }
      }
    };

    const changePrject = () => {
        let obj = model.projectList.find((item: any) => {
          return item.testItemId === model.projectId;
        });
        console.log('obj', obj);
        
        model.searchMsg.testItems = obj.testItemName;
    };
    let httpUrl: any = ref([]);
    onMounted(() => {
      let url: any = sessionStorage.getItem("httpUrl");
      httpUrl.value = JSON.parse(url);
      getProjectList();
    });

    return {
      reactInput,
      submitClick,
      changePrject,
      ...toRefs(model),
    };
  },
});
</script>

<style scoped>
.search_box {
  width: 100%;
  display: flex;
  align-items: center;
}
.lay-select {
  width: 20%;
}

.center {
  width: 200px;
}

.w {
  width: 200px;
  margin-left: 28px;
}

.lay-select {
  width: 200px;
}
.lay-row {
  width: 100%;
}
.btn {
  margin: 0 18px;
}
</style>